package com.zego.wawaji.ui.play;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.noble.library.observer.MsgMgr;
import com.noble.library.observer.PObserver;
import com.noble.library.time.FormatPattern;
import com.noble.library.time.TimeUtil;
import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.base.BasePanel;
import com.zego.wawaji.ui.base.CustomFrameLayout;
import com.zego.wawaji.ui.bean.wawa.WawaJi;
import com.zego.wawaji.ui.play.bean.LuckyUser;
import com.zego.wawaji.ui.play.bean.LuckyUserList;
import com.zego.wawaji.wawaji_client.cmd.JsonKey;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 游戏页Foot布局
 * Created by Su on 2017/11/25.
 */
public class PlayFoot extends BasePanel implements PObserver, RequestComplete {
    private CustomFrameLayout customFrameLayout;
    private PlayFootAdapter footAdapter;
    private RecyclerView recyclerView;
    private List<LuckyUser> dataList;
    private WawaJi mRoom;

    public PlayFoot(Context context, WawaJi mRoom) {
        super(context);
        setContentView(R.layout.play_foot_panel);
        this.mRoom = mRoom;
        initView();
        reqData();
    }

    private void reqData() {
        ModuleMgr.getWawaMgr().reqGameCatch(mRoom.getId(), this);
    }

    private void initView() {
        dataList = Collections.synchronizedList(new ArrayList<LuckyUser>());
        customFrameLayout = (CustomFrameLayout) findViewById(R.id.custom_record);
        customFrameLayout.show(R.id.play_no_record);

        recyclerView = (RecyclerView) findViewById(R.id.play_record);
        LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true);
        recyclerView.setLayoutManager(manager);
        footAdapter = new PlayFootAdapter();
        recyclerView.setAdapter(footAdapter);
        footAdapter.setList(dataList);

        MsgMgr.getInstance().attach(this);
    }

    /**
     * 处理最近抓中记录
     */
    public void handleSucResult(Map<String, Object> data) {
        if (data == null) return;

        String head = (String) data.get(JsonKey.KEY_USER_AVATAR);
        String name = (String) data.get(JsonKey.KEY_USER_NICKNAME);

        LuckyUser user = new LuckyUser();
        user.setUserHead(head);
        user.setUserName(name);
        user.setCreatTime(TimeUtil.formatTime(System.currentTimeMillis(),
                FormatPattern.FP_YMD_HMS));
        refreshList(user);
    }

    private void refreshList(LuckyUser user) {
        customFrameLayout.show(R.id.play_record);
        try {
            if (dataList.size() >= 5) {
                dataList.remove(0);
            }
            dataList.add(user);
            footAdapter.notifyDataSetChanged();
        } catch (Exception e) {
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Game_Suc:
                if (value != null)
                    handleSucResult((Map<String, Object>) value);
                break;
        }
    }

    public void doLogout() {
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqGameCatch) {
            if (response.isOk()) {
                LuckyUserList list = (LuckyUserList) response.getBaseData();
                if (list.getLuckyUserList().size() <= 0) return;
                for (LuckyUser user : list.getLuckyUserList()) {
                    refreshList(user);
                }
            }
        }
    }
}
